/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.base.util.biz;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.p2p.base.model.Money;
import com.p2p.base.util.DateUtil;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
 * 2015年4月2日 下午2:05:53
 */
public class ExcelUtil {

	public static <T> void getMapModelViewList(List<T> beanList,List<String> nameList,HSSFSheet sheet){
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return ;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       

        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getDeclaredMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
        int i=1;
		for(T bean:beanList){
			HSSFRow row = sheet.createRow(i);
			i++;
			Map<String,Object> data = new HashMap<String,Object>();
			//list.add(data);
			int j =0;
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){
				 
				
				try {
					Object o=entry.getValue().invoke(bean);
					String viewStr = null;
					if(o!=null){
						if(o instanceof Date){
							//viewStr = DateUtil.getDateTime(o);
							HSSFCell cell = row.createCell(j);
							cell.setCellValue((Date)o);
						}else if(o instanceof Money){
							
							Double yuan = ((Money)o).getYuan();
							HSSFCell cell = row.createCell(j);
							cell.setCellValue(yuan);
						}
						else{
							HSSFCell cell = row.createCell(j);
							cell.setCellValue(o.toString());
						}
					}
					
					data.put(entry.getKey(), viewStr);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				j++;
			}
			
		}
		
		return ;
		
	}
	
	public static void filldExcelByNameList(List<Map<String,Object>> beanList,List<String> nameList,HSSFSheet sheet,int startRow){
		int i = startRow;
		for(Map<String,Object> bean:beanList){
			HSSFRow row = sheet.createRow(i);
			i++;
			int j =0;
			for(String name:nameList){
				Object o = bean.get(name);
				
				if(o!=null){
					if(o instanceof Date){
						//viewStr = DateUtil.getDateTime(o);
						HSSFCell cell = row.createCell(j, HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(DateUtil.getDateTime(o));
					}else if(o instanceof Money){
						
						Double yuan = ((Money)o).getYuan();
						HSSFCell cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
						cell.setCellValue(yuan);
					}else if(o instanceof Number){
						/*String tempName = name.toLowerCase();
						if(tempName.endsWith("time")){
							
						}*/
						HSSFCell cell = row.createCell(j, HSSFCell.CELL_TYPE_NUMERIC);
						cell.setCellValue(o.toString());
					}
					else{
						HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
						cell.setCellValue(o.toString());
					}
				}else{
					HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_BLANK);
				}
				j++;
			}
		}
		
	}
}
